/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-common
*
* @File name : WX_TOKEN.java
*
* @Author : Administrator
*
* @Date : 2017年8月17日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年8月17日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package org.marketing.common;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;


import net.sf.json.JSONObject;


/**
*
* @author Administrator
* TODO description
* @date 2017年8月17日
*/

public class WX_TOKEN {
    @Value("${APPID}")
    private String                    appid;
    @Value("${SECRET}")
    private String                    secret;
    @Value("${REQ-URL}")
    private String                    reqUrl;
    
    private Logger logger=Logger.getLogger(WX_TOKEN.class);
    
    public String genAccessToken() {
        logger.info("----------------gen wechatAccessToken");
        if (StringUtils.isEmpty(appid) || StringUtils.isEmpty(secret))
            return "";
        reqUrl = reqUrl.replace("APPID", appid).replace("APPSECRET", secret);
        System.out.println(reqUrl);
        
        String sendGetRequest = HttpClientUtil.sendGetRequest(reqUrl);
        JSONObject fromObject = JSONObject.fromObject(sendGetRequest);
        AccessToken.setAccessToken(fromObject.get("access_token")
                .toString());
        AccessToken.setExpiresIn(fromObject.get("expires_in")
                .toString());
        return AccessToken.getAccessToken();
        
    }
}
